#!/usr/bin/python
# -*- coding: UTF-8 -*- 
import win32con,win32api,re,os
def get_Reg_key(path,key_):
    reg_root = win32con.HKEY_LOCAL_MACHINE
    #reg_root = win32con.HKEY_CLASSES_ROOT
    reg_path = r'%s' % path
    reg_flags = win32con.KEY_READ | win32con.KEY_WOW64_64KEY
    # 读取键值
    key = win32api.RegOpenKey(reg_root, reg_path, 0, reg_flags)
    value, key_type = win32api.RegQueryValueEx(key, key_)
    
    #关闭键
    win32api.RegCloseKey(key)
    return {"value":value}

########## main ##########################
try:
    reg=get_Reg_key("SOFTWARE\Classes\CATIA.Part\shell\open\command","")
except Exception as e:  
    print("找不到注册表信息，请检查Catia是否安装正确")
    print(e)
else:
    print(reg)
    

    # EnvPath=re.sub(r'(-env) (.*) (-direnv)', "", name)
    EnvfName=re.search(r'(-env) (.*) (-direnv)', str(reg))
    EnvPath=re.search(r'(-direnv) "(.*?)"', str(reg))
    print(EnvfName)
    print(EnvfName.groups())
    print("全局环境变量文件名== "+EnvfName.groups()[1])
    print("全局环境变量路径== "+EnvPath.groups()[1])

    # {'value': '"D:\\Program Files (x86)\\CatiaV5-6R2020\\win_b64\\code\\bin\\CNEXT.exe" -env CATIA_P3.V5-6R2020.B30 -direnv "D:\\Program Files (x86)\\ProgramData\\DassaultSystemes\\CATEnv" -object "%1"'}
finally:    
    os.system("pause")
    # D:\Program Files (x86)\ProgramData\DassaultSystemes\CATEnv